﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.User;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Entity;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.Device.Repositories;

/// <summary>
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class AppCheckinAuthRepositoty : Repository<AppCheckinAuth, DefaultDbContext>
{
    private readonly IApplicationContext applicationContext;
    public AppCheckinAuthRepositoty(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
    {
        this.applicationContext = applicationContext;
    }

    public IPagedList<AppCheckinAuth> GetAppCheckinAuths(AppCheckinAuthQuery query, int pageIndex, int pageSize)
    {
        Sql sql = Sql.Builder;
        sql.Select("app_checkin_auth.id")
            .From("app_checkin_auth");
        #region 主管单位数据权限
        if (applicationContext.CurrentUser is IDeptUser deptUser && !deptUser.IsSuperAdministrator())
        {
            sql.InnerJoin("project as prj").On($"app_checkin_auth.project_id=prj.project_id and prj.is_deleted=0 and prj.industry_type=@0 ", deptUser.IndustryType);
            if (deptUser.Level == AdministrativeLevel.City)
                sql.Append($" and prj.city_code={deptUser.CityCode}");
            if (deptUser.Level == AdministrativeLevel.Area)
                sql.Append($" and prj.area_code={deptUser.AreaCode}");
        }
        #endregion

        if (!string.IsNullOrEmpty(query.Keywords)) {
            sql.InnerJoin("project as p").On("app_checkin_auth.project_id=p.project_id");
            sql.InnerJoin("corp as c").On("app_checkin_auth.corp_id=c.corp_id");
            sql.Where("(c.name Like @0 or p.name Like @0 )", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
        }

        if (query.CorpId.HasValue)
            sql.Where("app_checkin_auth.corp_id = @0", query.CorpId);
        if (query.ProjectId.HasValue)
            sql.Where("app_checkin_auth.project_id = @0", query.ProjectId);
        if (query.Status.HasValue)
            sql.Where("app_checkin_auth.status = @0", query.Status);
        sql.OrderBy("app_checkin_auth.create_time DESC");
        return GetPagingEntities(pageSize, pageIndex, sql);
    }

    public IEnumerable<AppCheckinAuth> GetAppCheckinAuthsByProjectId(int projectId)
    {
        int areaVersion = EntityCacheService.GetAreaVersion("ProjectId", projectId);
        return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
        {
            return $"AppCheckinAuthsByProjectId:{projectId}:{areaVersion}";
        }, () =>
        {
            Sql sql = Sql.Builder
            .Select("id")
            .From("app_checkin_auth")
            .Where("project_id=@0", projectId);
            return sql;
        });
    }

    public bool Exists(AppCheckinAuth entity, int? id)
    {
        if (id.HasValue)
            return DbContext.Exists<AppCheckinAuth>("corp_id=@0 and project_id=@1 and id!=@2", entity.CorpId, entity.ProjectId, id);
        return DbContext.Exists<AppCheckinAuth>("corp_id=@0 and project_id=@1", entity.CorpId, entity.ProjectId);
    }
}